//上传图片
//引进multer模块  记得在终端安装multer模块
const multer = require("multer");
const uuid = require("uuid/v1");
const fs = require("fs");
const path = require("path");
// 开始配置
const storage = multer.diskStorage({
    //上传图片的路径，是在你的静态目录下（public）uploads会自动进行创建
    destination: function(req, file, callback) {
        if (file.mimetype === "audio/mpeg") {
            let account = req.headers.account,
                address = `${path.dirname(path.dirname(__dirname))}/uploads/${account}`;
            if (!fs.existsSync(address)) {
                fs.mkdirSync(address);
            }
            callback(null, address);
        } else {
            let address = `${path.dirname(path.dirname(__dirname))}/uploads/imgs`;
            if (!fs.existsSync(address)) {
                fs.mkdirSync(address);
            }
            callback(null, address);
        }
    },
    //给上传文件重命名，获取添加后缀名
    filename: function(req, file, callback) {
        //在这里我是把图片命名为我当前登陆的用户名
        // console.log(file); // file.mimetype
        let day = new Date().Format("yyyyMMhhmmss");
        let uid = uuid();
        let name = `${uid}.${day}.${file.originalname}`;
        if (file.mimetype != "audio/mpeg") global.uploadImage = name;
        else global.uploadVoice = name;
        callback(null, name);
    }
});
//导出模块
module.exports = multer({ storage });
